package isssr.boundary.src;

import isssr.control.exception.GoalElicitationException;
import isssr.control.src.OrganizationalGoalCRUDControl;
import isssr.entity.src.GoalStatus;
import isssr.entity.src.OrganizationalGoalDTO;
import isssr.entity.src.User;
import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;



/**
 * @author Ludovico 
 *
 */
@ManagedBean(name="organizationalGoalCRUDBoundary")
@ViewScoped
public class OrganizationalGoalCRUDBoundary implements Serializable {

	
	private static final long 		serialVersionUID = -1639892744600496342L;
	private boolean 				render_save_action;
	private boolean 				render_delete_action;
	private boolean 				render_close_action;
	private boolean 				render_goal;
	private boolean					save_warning;
	private OrganizationalGoalDTO 	displayed_goal;
	
	
	//injected bean 
	@ManagedProperty(value="#{organizationalGoalCRUDControl}")
	private OrganizationalGoalCRUDControl ctrl; 
	
	 
	public OrganizationalGoalCRUDBoundary(){
		System.out.println("CRUD BOUNDARY CREATED");
		this.render_goal 	= false;
		this.save_warning 	= false;
		this.disableActionMenu();
	}
	
	
	public void setCtrl(OrganizationalGoalCRUDControl ctrl) {
		this.ctrl = ctrl;
	}


	public boolean isRender_save_action() {
		return render_save_action;
	}


	public void setRender_save_action(boolean render_save_action) {
		this.render_save_action = render_save_action;
	}


	public boolean isRender_delete_action() {
		return render_delete_action;
	}

	
	public void setRender_delete_action(boolean render_delete_action) {
		this.render_delete_action = render_delete_action;
	}


	public boolean isRender_close_action() {
		return render_close_action;
	}


	public void setRender_close_action(boolean render_close_action) {
		this.render_close_action = render_close_action;
	}


	public boolean isRender_goal() {
		return render_goal;
	}


	public void setRender_goal(boolean render_goal) {
		this.render_goal = render_goal;
	}


	public OrganizationalGoalDTO getDisplayed_goal() {
		return displayed_goal;
	}


	public void setDisplayed_goal(OrganizationalGoalDTO displayed_goal) {
		this.displayed_goal = displayed_goal;
	}


	public void disableActionMenu()
	{
		this.setRender_save_action(false);
		this.setRender_delete_action(false);
		this.setRender_close_action(false);
	}

	
	public void enableActionMenu()
	{
		this.setRender_save_action(true);
		this.setRender_delete_action(true);
		this.setRender_close_action(true);
	}


	public void setGoalEnactor(User goalEnactor)
	{
		if(this.displayed_goal != null)
			this.displayed_goal.setGoalEnactor(goalEnactor);
	}
	
	
	public User getGoalEnactor() {
		if(this.displayed_goal != null)
			return this.displayed_goal.getGoalEnactor();
		else
			return null;
	}
	
	
	public void setSuperOwnerList(List<User> superOwnerList) {
		
		if(this.displayed_goal != null && superOwnerList != null)
			this.displayed_goal.setSuperList(superOwnerList);
	}

	
	public List<User> getSuperOwnerList() {
		if(this.displayed_goal != null)
			return this.displayed_goal.getSuperList();
		else
			return null;
	}

	
	public void createGoal(){
		
		try {
			this.displayed_goal = this.ctrl.createGoal();
			if(this.displayed_goal != null)
			{
				this.enableActionMenu();
				this.setRender_goal(true);
				this.sendMessage("Info", this.displayed_goal.getTitle() + " successfully created!", FacesMessage.SEVERITY_INFO);
			}
		} catch (GoalElicitationException e) {
			//compose error message
		}
		
	}
	
	
	public void readGoal(String title){
		if(title != null)
		{
			System.out.println("goal title:\t"+title);
			try {
				this.displayed_goal = this.ctrl.readGoal(title);
				if(this.displayed_goal != null)
				{
					this.enableActionMenu();
					this.setRender_goal(true);
					this.sendMessage("Info", this.displayed_goal.getTitle() + " successfully opened!", FacesMessage.SEVERITY_INFO);				
				}
			} catch (GoalElicitationException e) {
				//compose error message
			}
		}
	}
	
	
	public void updateGoal(){
		if(this.displayed_goal != null)
		{
			try {
				this.displayed_goal = this.ctrl.updateGoal(this.displayed_goal);
				if(this.displayed_goal != null)
				{
					this.enableActionMenu();
					this.setRender_goal(true);
					this.sendMessage("Info", this.displayed_goal.getTitle() + " successfully saved!", FacesMessage.SEVERITY_INFO);				
				}
			} catch (GoalElicitationException e) {
				// compose error message
			}	
		}
		else
		{// send alert message
		}	
	}
	
	
	public void deleteGoal(){
		try {
			this.ctrl.deleteGoal();
			this.closeGoal();
		} catch (GoalElicitationException e) {
			// compose error message	
		}
	}
	
	
	public void closeGoal(){
		this.displayed_goal = null;
		this.disableActionMenu();
		this.setRender_goal(false);
	}

	
	public void closeGoalWithSave() {
		this.updateGoal();
		this.closeGoal();
	}
	
	
	private void sendMessage(String summary, String detail, Severity severity)
	{
		FacesMessage message = new FacesMessage(summary, detail);
		message.setSeverity(severity);
		FacesContext context = FacesContext.getCurrentInstance();  
		context.addMessage(null, message);  
	}
	

	private String getSuggestedGoalName(String baseName, String userName)
	{
		String suggestedName = "";
		String separator = "_";
		
		if(baseName == null)
			suggestedName.concat("Organzational_Goal");
		else 
			suggestedName = baseName;
		
		
		if(userName != null)
		{
			suggestedName.concat(separator);
			suggestedName.concat(userName);
		}
		
		Integer randomNumber = new Integer((int)Math.random()*1000);
		{
			suggestedName.concat(separator);
			suggestedName.concat(randomNumber.toString());
		}
		
		return suggestedName;
	}
	
	
}
